//
//  CustomTextAlertView.m
//  DepressionAuxiliary
//
//  Created by XTW on 2021/5/13.
//

#import "CustomTextAlertView.h"
#import "AppDelegate.h"
#import "YYTextView.h"
#import "IQKeyboardManager.h"

@interface CustomTextAlertView()

@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIView *showView;
@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation CustomTextAlertView

- (id)initItem:(NSString *)title withButtons:(NSArray *)btns{
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomTextAlertView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        self.titleLabel.text = title;
        float width = 260 * SCREEN_SCALE_320;
        [self.showView setFrame:CGRectMake((SCREEN_WIDTH_DEVICE-width)/2.0, (SCREEN_HEIGHT_DEVICE-280)/2.0, width, 280)];
        self.showView.layer.masksToBounds = YES;
        self.showView.layer.cornerRadius = 20.0f;
        
        self.textView.textColor = TextColor;
        self.textView.font = [UIFont systemFontOfSize:14.0f];
        
        if (btns.count == 1) {
            self.leftBtn.hidden = YES;
            [self.rightBtn setFrame:CGRectMake((width-108)/2.0, 200, 108, 40)];
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.rightBtn setTitle:[btns firstObject] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundColor:ThemeColor];
        } else {
            float widthButton = (width - 80 - 20)/2.0;
            [self.leftBtn setFrame:CGRectMake(40, 200, widthButton, 40)];
            [self.rightBtn setFrame:CGRectMake(40 + widthButton + 20, 200, widthButton, 40)];
            self.leftBtn.layer.masksToBounds = YES;
            self.leftBtn.layer.cornerRadius = 16.0f;
            self.leftBtn.layer.borderWidth = 1.0f;
            self.leftBtn.layer.borderColor = ThemeColor.CGColor;
            [self.leftBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            self.leftBtn.layer.masksToBounds = YES;
            self.leftBtn.layer.cornerRadius = 20.0f;
            [self.leftBtn setTitle:[btns firstObject] forState:UIControlStateNormal];

            [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.rightBtn setTitle:[btns objectAtIndex:1] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundColor:ThemeColor];
        }
        
        
        if (SCREEN_SCALE_320 == 1) {
            self.leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
            self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
        }
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)show {
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [del.window addSubview:self];
    self.showView.layer.opacity = 0.5f;
    self.showView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
                         self.showView.layer.opacity = 1.0f;
                         self.showView.layer.transform = CATransform3DMakeScale(1, 1, 1);
                     }
                     completion:NULL
     ];
}

- (IBAction)leftPress {
    if (self.CancelHandle) {
        self.CancelHandle();
    }
    [self removePress];
}

- (IBAction)rightPress {
    if ([self.rightBtn.titleLabel.text isEqualToString:@"取消"]) {
        if (self.CancelRequestHandle) {
            self.CancelRequestHandle();
        }
    } else {
        if (self.SureHandle) {
            self.SureHandle(self.textView.text);
        }
    }
    
    [self removePress];
}

- (IBAction)removePress {
    [self removeFromSuperview];
}

- (void)backgroundClickDisable {
    self.removeBtn.userInteractionEnabled = NO;
}

- (void)backgroundClickEnable {
    self.removeBtn.userInteractionEnabled = YES;
}


// Handle keyboard show/hide changes
- (void)keyboardWillShow: (NSNotification *)notification {
    CGSize screenSize = [self countScreenSize];
    CGSize dialogSize = [self countDialogSize];
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                     animations:^{
                         self.showView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
                     }
                     completion:nil
     ];
}

- (void)keyboardWillHide: (NSNotification *)notification {
    CGSize screenSize = [self countScreenSize];
    CGSize dialogSize = [self countDialogSize];
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                     animations:^{
        self.showView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
                     }
                     completion:nil
     ];
}

- (CGSize)countScreenSize {
    return CGSizeMake(SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE);
}

- (CGSize)countDialogSize {
    return CGSizeMake(self.showView.frame.size.width, self.showView.frame.size.height);
}
@end
